package com.funsport.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.funsport.entity.VenueBooking;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 场地预订Mapper
 */
@Mapper
public interface VenueBookingMapper extends BaseMapper<VenueBooking> {
    
    /**
     * 检查时间段是否冲突
     * 
     * @param venueId 场地ID
     * @param startTime 开始时间
     * @param endTime 结束时间
     * @param excludeBookingId 排除的预订ID（用于更新时）
     * @return 冲突的预订数量
     */
    @Select({
        "<script>",
        "SELECT COUNT(*) FROM venue_bookings",
        "WHERE venue_id = #{venueId}",
        "AND status IN ('pending', 'confirmed')",
        "AND (",
        "  (start_time &lt; #{endTime} AND end_time &gt; #{startTime})",
        ")",
        "<if test='excludeBookingId != null'>",
        "  AND id != #{excludeBookingId}",
        "</if>",
        "</script>"
    })
    int checkTimeConflict(@Param("venueId") Long venueId,
                          @Param("startTime") LocalDateTime startTime,
                          @Param("endTime") LocalDateTime endTime,
                          @Param("excludeBookingId") Long excludeBookingId);
    
    /**
     * 获取场地在指定日期的所有预订
     */
    @Select({
        "SELECT * FROM venue_bookings",
        "WHERE venue_id = #{venueId}",
        "AND DATE(start_time) = DATE(#{date})",
        "AND status IN ('pending', 'confirmed')",
        "ORDER BY start_time"
    })
    List<VenueBooking> getBookingsByVenueAndDate(@Param("venueId") Long venueId,
                                                   @Param("date") LocalDateTime date);
}

